<?php 
class Session
{
	private $sessionData	= array();
	
	private $sessLife		= 1440;

	/**
	 * This class saves the PHP session data in a database table.
	 */
    function __construct($sessLife = '')
    {
        $this->m		= new Model_Session();
		
		if (!$sessLife)
			$sessLife	= @ini_get('session.gc_maxlifetime');

		if ($sessLife) $this->sessLife	= $sessLife;		
    }
    
    /**
	 * open the session.
	 */
    function open ($save_path, $session_name)
    {
        // perform garbage collection
		$flag = rand(1,100);
		
		if($flag == 88){
			$this->gc($this->sessLife);
		}
		
        return TRUE;
    }
    
   /**
    * close the session.
    */
    function close ()
    {
    	if (!empty($this->sessionData)){
            // perform garbage collection
            $result = $this->gc($this->sessLife);
			
            return $result;
        }        
        return FALSE;
    }
    
	/**
	 * read any data for this session.
	 */
    function read ($session_id)
    {
		$where = "WHERE session_id='".addslashes($session_id) ."'";
		
        $this->sessionData = $this->m->getRow($where);
		
        if (isset($this->sessionData['sessionData'])){
     		return $this->sessionData['sessionData'];
        } else {
            return '';
        }        
    }
    
    /**
	 * write session data to the database.
	 */
    function write ($session_id, $sessionData)
    {
        if (!empty($this->sessionData)) {
            if ($this->sessionData['session_id'] != $session_id) {
                // user is starting a new session with previous data
                $this->sessionData = array();
            }
        }
        if (empty($this->sessionData)) {
            // create new record
            $array['session_id'] = $this->sessionData['session_id'] = $session_id;
            $array['date_created'] = time();
            $array['last_updated'] = time();
            $array['sessionData'] = addslashes($sessionData);

			$res = $this->m->insert($array);
            
        } else {
            // update existing record
            if (isset($_SESSION['logon_user_id'])) {
                $array['user_id']  = $_SESSION['logon_user_id'];
            } else {
          		$array['user_id']  = 0;
          	}
            $array['last_updated'] = time();
            $array['sessionData'] = addslashes($sessionData);

			$where = 'WHERE session_id = "'.$this->sessionData['session_id'].'"';

			$res = $this->m->update($array, $where);
			
        }
		return $res;
        
    }

	/**
	 * destroy the specified session.
	 */
    function destroy($session_id)
    {
		$this->m->del('WHERE session_id = "'.$session_id.'"');
        return TRUE;
    }

	/**
	 * perform garbage collection.
	 */
	function gc($max_lifetime)
    {
        $real_now = time();

        $dt = $real_now - $max_lifetime;


        if($this->m->del("WHERE last_updated < ".$dt)) return TRUE;
		else return FALSE;
       
    }

	/**
	 * ensure session data is written out before classes are destroyed
	 */
    function __destruct ()
    {
		@session_write_close();
    }
} 